﻿using UnityEngine;
using System.Collections;

public class PasswordRoate : MonoBehaviour {
    public PasswordButton[] PassWordBtns; //所有按钮的控制脚本

    bool Move = false; //接收自按钮信息 是否开始转动
    float TargetRotaZ; //目标旋转角度
    const int PassWordLength = 8;  //密码长度
    string inputPassword = "";   //保存输入密码 用于比对正确密码
    string Password = "12345678";  //正确密码
    /// <summary>
    /// 输入正确后的操作
    /// </summary>
    void InputCorrect()
    {
    }
    /// <summary>
    /// 输入错误之后的操作
    /// </summary>
    void InputWrong()
    {
        foreach (PasswordButton aPasswordBtn in PassWordBtns)
        {
            aPasswordBtn.enabled = true;
            aPasswordBtn.Checkout(); //按钮复位
        }
        TargetRotaZ = 0; //转轴归位
        inputPassword = "";//输入密码复位
    }
    /// <summary>
    /// 判断输入的正误
    /// </summary>
    public void CompletedInput()
    {
        if (inputPassword.Length == PassWordLength)
        {
            if (inputPassword.Equals(Password))
            {
                InputCorrect();
            }
            else InputWrong();
        }
    }

    /// <summary>
    /// 接收按钮按下信息进行操作
    /// </summary>
    /// <param name="TZ"></param>
	public void GetBtnMessage(int TZ)
    {
        TargetRotaZ = (TZ - 1) * 45;//计算目标角度
        Move = true;                //开始转动
        inputPassword += TZ.ToString();//更新输入密码
    }
    /// <summary>
    /// 旋转自身
    /// </summary>
    void RotateMyself()
    {
        float RotateAngle = Mathf.Lerp(transform.rotation.eulerAngles.z, TargetRotaZ, 5 * Time.deltaTime) - transform.rotation.eulerAngles.z;
        transform.Rotate(0f, 0f, RotateAngle);
    }
	void Update () {
        if (Move)
        {
            RotateMyself();
        }
    }
}
